package com.qire.manhua.dialog;

import android.content.Context;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CompoundButton;

import com.qire.common.basic.StringUtil;
import com.qire.common.support.base.BaseDialog;
import com.qire.common.utils.ToastHelper;
import com.qire.manhua.R;
import com.qire.manhua.activity.helper.LoginRouter;
import com.qire.manhua.databinding.DialogCommentComplaintBinding;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

/**
 * 举报评论弹窗：在所有评论处点击举报时弹出
 */
public class CommentComplaintDialog extends BaseDialog {

    private final String FORMAT = "<font color=#409ecd>@%s</font>:%s";

    private final DialogCommentComplaintBinding viewDataBinder;

    public final ObservableField<CharSequence> commentContent = new ObservableField<>();

    public final ObservableField<String> complaintContent = new ObservableField<>();

    private int check_number = -1;
    private CompoundButton lastRadioButton;
    private CommentReportDialogListener listener;

    public CommentComplaintDialog(@NonNull Context context) {
        super(context, R.style.BottomDialog);
        viewDataBinder = DialogCommentComplaintBinding.inflate(LayoutInflater.from(context));
        viewDataBinder.setCommentComplaintDialog(this);
        setContentView(viewDataBinder.getRoot());
    }

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked) {
            check_number = StringUtil.valueToInt(buttonView.getTag(), check_number);
            if(lastRadioButton != null) {
                lastRadioButton.setChecked(false);
            }
            lastRadioButton = buttonView;
        }
    }

    public void closeBtnOnClick(View view) {
        dismiss();
    }

    public void complaintBtnOnClick(View view) {
        if(listener != null) {
            if(LoginRouter.ROUTER.checkLoginRouteIfNeed()) {
                ToastHelper.showToast("请先登录！");
                return;
            }
            if(check_number <= 0) {
                ToastHelper.showToast("请单选一个举报类型！");
                return;
            }
            String complaintContentText = complaintContent.get();
            if(StringUtil.isEmpty(complaintContentText)) {
                ToastHelper.showToast("请填写举报理由！");
                return;
            }
            listener.onReportClick(check_number, complaintContentText);
        }
        dismiss();
    }

    public void setListener(CommentReportDialogListener listener){
        this.listener = listener;
    }

    public void setCommentInfo(String author, CharSequence comment){
        commentContent.set(Html.fromHtml(String.format(FORMAT, author, comment)));
    }

    @Override
    public void show() {
        super.show();
        //Grab the window of the dialog, and change the width
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        Window window = this.getWindow();
        lp.copyFrom(window.getAttributes());
        //This makes the dialog take up the full width
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);
    }


    public interface CommentReportDialogListener{
        void onReportClick(int number, String text);
    }
}
